//购物车管理
var Module = (function (Module) {
    var old_options = {
        url: '',
        num: 1,
        data: {},
        success: null,
        showError: true
    };
    Module.cartManager = {
        //添加到购物车
        add: function (options, request) {
            var options = $.extend({}, old_options, options);
            if (request) {
                request(options);
            }
        },
        //获取购物车数量 从cookie中读取
        getCartNumForCookie: function () {
            var cartQuantity = $.cookie("cq");
            cartQuantity = parseInt(cartQuantity);
            return !isNaN(cartQuantity) ? cartQuantity : 0;
        }
    };
    return Module;
})(Module || {});
//请求管理
var Module = (function (Module) {
    Module.request = {
        //ajax请求
        ajax: function (options) {
            var p = options || {};
            var url = p.url;
            $.ajax({
                cache: false,
                async: p.async == undefined ? true : p.async,
                url: url,
                data: p.data,
                dataType: p.dataType || 'json',
                type: p.type || 'POST',
                beforeSend: function () {
                    if (options.loading) {
                        oDiaglor.wait(options.loading);
                    }
                    if (options.beforeSend) {
                        options.beforeSend();
                    }
                },
                complete: function () {
                    if (options.loading) {
                        oDiaglor.close();
                    }
                    if (options.complete) {
                        options.complete();
                    }
                },
                success: function (result) {
                    if (p.success) {
                        p.success(result);
                    }
                },
                error: function (xhr, status) {
                    oDiaglor.removeMessage();
                    if (p.error) {
                        p.error(xhr, status);
                    }
                    if (xhr.status == 403) {
                        $.fn.loginFrame();
                    }
                    else if (p.showError) {
                        if (xhr.status == 400) {
                            var responseError = eval(xhr.responseText);
                            var info = '';
                            for (var i = 0; i < responseError.length; i++) {
                                info += responseError[i] + '<br/>';
                            }
                            oDiaglor.prompt({ message: info });
                        }
                        else {
                            oDiaglor.prompt({ message: '系统出错' });
                        }
                    }
                }
            });
        }
    };
    return Module;
})(Module || {});
//运动效果管理
var Module = (function (Module) {
    var old_options = {
        startX: 0,
        startY: 0,
        endX: 0,
        endY: 0,
        imgUrl: '',
        imgHeight: 0,
        imgWidth: 0,
        wrapeBox: 'body',
        topAnimateType: 'easeInQuad',
        leftAnimateType: '',
        callBack: null //回调方法
    };
    Module.animatManger = {
        //创建小图片 并运动到指定位置
        createImgAndMoveToTarget: function (options) {
            var options = $.extend({}, old_options, options);
            var $_img = $("<img/>");
            $_img.attr('src', options.imgUrl).css({
                width: options.imgWidth,
                height: options.imgHeight,
                position: 'absolute',
                top: options.startY,
                left: options.startX
            });
            $(options.wrapeBox).append($_img);
            //开始运动
            $_img.animate({
                top: [options.endY, options.topAnimateType],
                left: [options.endX, options.leftAnimateType]
            }, 1000, function () {
                //如果存在回调方法 则调用回调 传入 生成的图片对象
                $_img.remove();
                if (options.callBack) {
                    options.callBack(options);
                }
            });
        }
    };
    return Module;
})(Module || {});
//面板管理
var Module = (function (Module) {
    var old_options = {
        panelName: '.bar-panel',
        openWithAnimate: true,
        openOrientation: 'left',
        animateTime: 500,
        openBtn: '.open-panel',
        openStyleClassForBtn: 'selected',
        closeBtn: '.bar-panel-close',
        waitWrap: '.panel-wait-load'
    };
    Module.Panel = function (options) {
        return new Panel(options);
    };
    function Panel(options) {
        this.options = $.extend({}, old_options, options);
        this.$_panel = $(this.options.panelName);
        this.$_openBtn = $(this.options.openBtn);
        this.$_closeBtn = $(this.options.closeBtn);
        this.$_waitWrap = $(this.options.waitWrap);
        this.isOpen = false; //是否打开
        this._observer = []; //观察者
    }
    Panel.prototype = {
        init: function () {
            var _this = this;
            this.$_openBtn.click(function () {
                $(this).addClass(_this.options.openStyleClassForBtn);
                _this.open();
            });
            this.$_closeBtn.click(function () {
                _this.$_openBtn.removeClass(_this.options.openStyleClassForBtn);
                _this.close();
            });
        },
        getPanelLeft: function () {
            return this.$_panel.position().left;
        },
        getPanelTop: function () {
            return this.$_panel.position().top;
        },
        getPanelWidth: function () {
            return this.$_panel.outerWidth(true);
        },
        //打开面板
        open: function () {
            if (this.isOpen)
                return false;
            this.$_panel.show();
            if (this.options.openWithAnimate) {
                var params = {
                    left: -this.getPanelWidth()
                };
                this.animatePanel(params, this.options.animateTime);
            }
            this.isOpen = true;
            this.notify(this);
        },
        //关闭面板
        close: function () {
            if (!this.isOpen)
                return;
            if (this.options.openWithAnimate) {
                var _this = this;
                var params = {
                    left: 0
                };
                this.animatePanel(params, this.options.animateTime, function () {
                    _this.$_panel.hide();
                    _this.isOpen = false;
                    _this.notify(_this);
                });
            }
            else {
                this.$_panel.hide();
                this.isOpen = false;
                this.notify(this);
            }
        },
        finish: function () {
            this.$_waitWrap.hide();
        },
        waitFinish: function () {
            this.$_waitWrap.show();
        },
        //面板运动
        animatePanel: function (params, animateTime, callback) {
            this.$_panel.animate(params, animateTime, function () {
                if (callback) {
                    callback();
                }
            });
        },
        //添加观察者
        addObserver: function (observer) {
            this._observer.push(observer);
        },
        //通知观察者
        notify: function (data) {
            for (var i = 0; i < this._observer.length; i++) {
                if (this._observer[i])
                    this._observer[i].update(data);
            }
        },
        delObserver: function (observer) {
            for (var i = 0; i < this._observer.length; i++) {
                if (this._observer[i] == observer) {
                    this._observer.splice(i, 1);
                }
            }
        },
        //清空观察者
        cleanObserver: function () {
            this._observer = [];
        }
    };
    return Module;
})(Module || {});
